package ModelLayer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class SalesRecords {

	private ArrayList<Sale> salesRecords;
	
	private static SalesRecords instance = null;
	
	private SalesRecords()
	{
		
		salesRecords = new ArrayList<Sale>();
	}

	public static SalesRecords getInstance()
	{
	    if(instance == null)
	        {
	            instance = new SalesRecords();
	        }
	    return instance;
	}
	
	//create and add an offer
	public int createSale(int customerId, int employeeId, long startDate) throws RealException
	{
		Sale sale = new Sale(customerId, employeeId, startDate);
		salesRecords.add(sale);
		return sale.getId();
	}
	
	// remove an order or offer
	public void removeSale(int id) throws RealException
	{
		Sale sale = getSale(id);
		if (sale != null) {
			sale.deleteSale();
			salesRecords.remove(sale);
		}
	}
	
	//update information about order or offer
	public void updateSale(int id, int choise, String value) throws RealException
	{
		Sale sale;
		sale = getSale(id);
		switch (choise) {
			case 1:
				sale.setEmployee(EmployeeRecords.getInstance().getEmployee(Integer.parseInt(value)));
				break;
			case 2:
				sale.setCustomer(CustomerRecords.getInstance().getCustomer(Integer.parseInt(value)));
				break;
			case 3:
				sale.setStartDate(Long.parseLong(value));
				break;
		}
	}
			
		
							
	//return a sale by its id
	public Sale getSale(int id) throws RealException
	{
		boolean found = false;
		Sale theSale = null;
		Iterator<Sale> it = salesRecords.iterator();
		while(it.hasNext() && !found)
		{
			Sale sale = it.next();
			if (id == sale.getId()) {
				theSale = sale;
				found = true;
			}
		}
		if(!found) throw new RealException("Sale is not found");
		return theSale;
	}
	
	
	public Sale getOffer(int id) throws RealException
	{
		boolean found = false;
		Sale theSale = null;
		Iterator<Sale> it = salesRecords.iterator();
		while(it.hasNext() && !found)
		{
			Sale sale = it.next();
			if (id == sale.getId() && sale.getStatus() == Status.Offer) {
				theSale = sale;
				found = true;
			}
		}
		if(!found) throw new RealException("Sale is not found");
		return theSale;
	}
	
	public Sale getOrder(int id) throws RealException
	{
		boolean found = false;
		Sale theSale = null;
		Iterator<Sale> it = salesRecords.iterator();
		while(it.hasNext() && !found)
		{
			Sale sale = it.next();
			if (id == sale.getId() && sale.getStatus() == Status.Order) {
				theSale = sale;
				found = true;
			}
		}
		if(!found) throw new RealException("Sale is not found");
		return theSale;
	}
	
	public ArrayList<Sale> getSalesRecords()
	{
		return salesRecords;
	}
	
	//print all offers information
	public String printAllOfferInfo()
	{
		String description = "";
		if (salesRecords.size() != 0) {
			for (Sale sale: salesRecords) {
				if (sale.getStatus() == Status.Offer)
					description = description + sale.toString(); 
			}
		}
		else {
			description = "\nNo offer is added.";
		}
		return description;
	}
	
	//print all orders information
	public String printAllOrderInfo()
	{
		String description = "";
		if (salesRecords.size() != 0) {
			for (Sale sale: salesRecords) {
				if (sale.getStatus() == Status.Order)
					description = description + sale.toString(); 
			}
		}
		else {
			description = "\nNo order is added.";
		}
		return description;
	}
	
	@SuppressWarnings("unchecked")
	public void load(ObjectInputStream obj_in) throws IOException, ClassNotFoundException
	{
		this.salesRecords = (ArrayList<Sale>) obj_in.readObject();
		Sale.setLastId(((Integer)obj_in.readObject()).intValue());
	}
	
	
	public void save(ObjectOutputStream obj_out) throws IOException
	{		
		obj_out.writeObject(salesRecords);
		obj_out.writeObject(new Integer(Sale.getLastId()));
	}
	
}
